<?php

/**
 * 商家控制器
 *
 * @author liminghui
 */

require_once 'CommonController.php';

class Admin_PartnerController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_Partner();
        $this->view->title = "管理中心";
    }

     /**
     *　商家列表
     * 
     * @return void
     */
    public function indexAction()
    {
        $time = time();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['code'] = trim($this->_request->get('code'));
        $vars['name'] = trim($this->_request->get('name'));
        $urls = $vars;

        if ($vars['code'] != '') {
            $wheres[] = "`code` LIKE '%" . addslashes($vars['code']) . "%'";
        }
        if ($vars['name'] != '') {
            $wheres[] = "`name` LIKE '%" . addslashes($vars['name']) . "%'";
        }
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_model->getTableName()}` {$where} ORDER BY `addTime` DESC";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }

    /**
     * 修改商家信息
     * 
     * @return void
     */
    public function updateAction()
    {
        $id = intval($this->_request->get('id'));
        $backUrl = trim($this->_request->get('backUrl'));

        $row = $this->_model->fetchRow("`id` = {$id}");
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        //提交表单的处理　
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['fullName'] = trim($this->_request->getPost('fullName'));
            $field['name'] = $filter->filter(trim($this->_request->getPost('name')));
            $field['url'] = $filter->filter(trim($this->_request->getPost('url')));
            $field['lastModifyTime'] = date('Y-m-d H:i:s');
            $field['notes'] = trim($this->_request->getPost('notes'));
            if (strlen($field['fullName']) == 0) {
                echo $this->view->message('商家全称不能为空，请重新填写。') ;
                exit;
            }
            if (strlen($field['name']) == 0) {
                echo $this->view->message('商家简称不能为空，请重新填写。') ;
                exit;
            }
            $this->_model->update($field, "`id` = {$id}");
            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功。", $backUrl) ;
            exit;
        }
    }

    /**
     * 添加商家信息
     * 
     * @return void
     */
    public function insertAction()
    {
        //提交表单的处理　
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['code'] = trim($this->_request->getPost('code'));
            $field['fullName'] = trim($this->_request->getPost('fullName'));
            $field['name'] = $filter->filter(trim($this->_request->getPost('name')));
            $field['url'] = $filter->filter(trim($this->_request->getPost('url')));
            $field['addTime'] = date('Y-m-d H:i:s');
            $field['lastModifyTime'] = $field['addTime'];
            $field['notes'] = trim($this->_request->getPost('notes'));
            if (preg_match("|[^0-9a-zA-Z_-]|", $field['code'])) {
                echo $this->view->message('编号只能包含字母、数字、下划线和“-”以内的字符。') ;
                exit;
            }
            if (strlen($field['code']) == 0) {
                echo $this->view->message('商家编号不能为空，请重新填写。') ;
                exit;
            }
            if (strlen($field['fullName']) == 0) {
                echo $this->view->message('商家全称不能为空，请重新填写。') ;
                exit;
            }
            if (strlen($field['name']) == 0) {
                echo $this->view->message('商家简称不能为空，请重新填写。') ;
                exit;
            }
            if ($this->_model->exists($field['code'])) {
                echo $this->view->message('编号已经存在，请重新填写。') ;
                exit;
            }
            $this->_model->insert($field);
            echo $this->view->message("操作成功。", $this->view->projectUrl(array('action'=>'index'))) ;
            exit;
        }
    }
 
    
    /**
     * 删除商家
     * 
     * @return void
     */
    public function deleteAction(){
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_model->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
}